package com.wufeng.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.wufeng.dto.R;
import com.wufeng.entity.Evaluate;
import com.wufeng.entity.EvaluateQuestion;
import com.wufeng.entity.QuestionOption;
import com.wufeng.service.EvaluateQuestionService;
import com.wufeng.service.EvaluateService;
import com.wufeng.service.QuestionOptionService;
import com.wufeng.utils.ScoreResultUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/evaluatequestion")
public class EvaluateQuestionController {

    @Autowired
    private EvaluateService evaluateService;

    @Autowired
    private EvaluateQuestionService evaluateQuestionService;

    @Autowired
    private QuestionOptionService questionOptionService;

    /**
     * 根据测试id获取问题列表
     *
     * @param evaluateId
     * @return
     */
    @GetMapping("/getQuestionsListByEvaluateId")
    public R getQuestionsListByEvaluateId(@RequestParam("evaluateId") Integer evaluateId) {

        List<EvaluateQuestion> list = evaluateQuestionService.list(new QueryWrapper<EvaluateQuestion>().eq("evaluateId", evaluateId));
        HashMap<String, Object> map = new HashMap<>();
        map.put("list", list);
        return R.ok(map);
    }


    /**
     * 根据evaluateId计算测试结果
     *
     * @param map 携带id和选择列表的map
     * @return
     */
    @PostMapping("/calculateAndReturnResultByEvaluateId")
    public R calculateAndReturnResultByEvaluateId(@RequestBody Map<String, Object> map) {

        String id = (String) map.get("evaluateId");
        Integer evaluateId = Integer.parseInt(id);
        //选项a ，b ,c ,d等等
        List options = (List) map.get("options");


        //问题列表
        List<EvaluateQuestion> questionList =
                evaluateQuestionService.list(new QueryWrapper<EvaluateQuestion>().
                        eq("evaluateId", evaluateId).orderByAsc("sort"));

        //获取问题ids
        List<Integer> questionIds = questionList.stream().
                map(question -> question.getQuestionid()).collect(Collectors.toList());


        //根据问题ids获取选项列表
        List<QuestionOption> questionOptionList =
                questionOptionService.list(new QueryWrapper<QuestionOption>().in("questionId", questionIds));
//        System.out.println(questionOptionList);

        // 将questionId相同的分到一组
        Map<Integer, List<QuestionOption>> questionOptionsMap = questionOptionList.stream()
                .collect(Collectors.groupingBy(QuestionOption::getQuestionid));
//        System.out.println(questionOptionsMap);

        Collection<List<QuestionOption>> values = questionOptionsMap.values();

        //提交的答案组数与问题组数不一致
        if (values.size() != options.size()) {
            return R.error("提交的答案组数与问题组数不一致");
        }

        int index = 0;
        double totalScore = 0;
        for (List<QuestionOption> value : values) {
            System.out.println("-------------" + value + "-------------");
            //每一组选项
            for (QuestionOption questionOption : value) {
                if (questionOption.getValue().equals(options.get(index))) {
                    if (questionOption.getScore() == null) {
                        return R.error("系统错误,选项分数不能为空");
                    }
                    totalScore += questionOption.getScore();
                    break;
                }
            }
            index++;
        }
        System.out.println("总得分:" + totalScore);
        HashMap<String, Object> resultMap = new HashMap<>();
        resultMap.put("totalScore", totalScore);

        Evaluate evaluate = evaluateService.getById(evaluateId);
        String result = ScoreResultUtil.getResult(totalScore, evaluate.getEvaresult());

        resultMap.put("result", result);



        return R.ok(resultMap);
    }

    /**
     * 插入EvaluateQuestion，返回evaluateid
     * @param evaluateQuestion
     * @return
     */
    @PostMapping("/insertEvaluateQuestion")
    public R insertEvaluateQuestion(@RequestBody EvaluateQuestion evaluateQuestion) {
        Integer evaluateQuestionId = evaluateQuestionService.saveEvaluateQuestion(evaluateQuestion);
        HashMap<String, Object> map = new HashMap<>();
        map.put("evaluateQuestionId",evaluateQuestionId);
        return R.ok(map);
    }
}
